import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'

export const getCategoryAsyncAction = createAsyncThunk('getCategory', async (payload, store) => {
  const res = await fetch('/category.json').then(res => res.json())
  return res
})

const videoSlice = createSlice({
  name: 'video',
  initialState: {
    category: [], // 视频播放列表
    speed: 1, // 播放速度
    autoPlay: true, // 自动播放下一集
  },
  reducers: {
    updateCategory (state, action) {
      state.category = action.payload
    }
  },
  extraReducers: (builder) => {
    // 处理getCategoryAsyncAction的异步操作
    builder.addCase(getCategoryAsyncAction.pending, (state, action) => {
      // console.log('getCategoryAsyncAction pending', action)
    }).addCase(getCategoryAsyncAction.fulfilled, (state, { payload }) => {
      // console.log('getCategoryAsyncAction fulfilled', payload)
      state.category = payload
    }).addCase(getCategoryAsyncAction.rejected, (state, action) => {
      // console.log('getCategoryAsyncAction rejected', action)
    })
  }
})

export default videoSlice.reducer
export const { updateCategory } = videoSlice.actions